package com.jie.store.context;

import com.jie.dto.UploadImageDTO;
import com.jie.store.UploadStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

import static com.jie.enums.UploadModeEnum.getStrategy;


/**
 * 上传策略上下文
 *
 * @author ican
 */
@Service
public class UploadStrategyContext {
    /**
     * 上传模式
     */
    @Value("${upload.strategy}")
    private String uploadStrategy;

    @Autowired
    private Map<String, UploadStrategy> uploadStrategyMap;

    /**
     * 上传文件内容
     *
     * @param file 文件
     * @return {@link String} 文件地址
     */
    public String executeUploadStrategy(MultipartFile file) {
        return uploadStrategyMap.get(getStrategy(uploadStrategy)).uploadFile(file);
    }
    /**
     * 上传图片内容并返回缩略图
     */
    public UploadImageDTO executeUploadStrategyImages(MultipartFile file){
        return uploadStrategyMap.get(getStrategy(uploadStrategy)).uploadFileImages(file);
    }
    /**
     * 上传视频内容 并返回视频链接
     */
    public String executeUploadStrategyVideo(MultipartFile file){
        return uploadStrategyMap.get(getStrategy(uploadStrategy)).uploadFileVideo(file);
    }
    public boolean removeFile(String url,String fileName){
        return uploadStrategyMap.get(getStrategy(uploadStrategy)).removeFileName(url,fileName);
    }


}